fontchooser: Fix initial font selection
authorMatthias Clasen <mclasen@redhat.com>
Tue, 2 Mar 2021 22:55:30 +0000 (17:55 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 2 Apr 2021 05:06:23 +0000 (01:06 -0400)
The change in 740559a54f to populate the list incrementally
broke initial font selection. Fix that, by trying to select
until the incremental filling is done.

Fixes: #3687
gtk/gtkfontchooserwidget.c

index 7e70bd1d587eafc781296d5c4728192c9387c2f0..be3b4cd027c122a6ee61c75371d48a248e743b28 100644 (file)
@@ -1095,6 +1095,9 @@ add_languages_from_font (GtkFontChooserWidget *self,
 }
 #endif
 
+static gboolean
+gtk_font_chooser_widget_ensure_matching_selection (GtkFontChooserWidget *self);
+
 /* We incrementally populate our fontlist to prevent blocking
  * the font chooser for a long time with expensive FcFontSort
  * calls in pango for every row in the list).
@@ -1135,6 +1138,9 @@ add_to_fontlist (GtkWidget     *widget,
 
   gtk_slice_list_model_set_size (model, n);
 
+  if (gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (self->selection)) == GTK_INVALID_LIST_POSITION)
+    gtk_font_chooser_widget_ensure_matching_selection (self);
+
   if (n == G_MAXUINT)
     return G_SOURCE_REMOVE;
   else
@@ -1348,7 +1354,7 @@ my_pango_font_family_equal (const char *familya,
   return g_ascii_strcasecmp (familya, familyb) == 0;
 }
 
-static void
+static gboolean
 gtk_font_chooser_widget_ensure_matching_selection (GtkFontChooserWidget *self)
 {
   const char *desc_family;
@@ -1358,7 +1364,7 @@ gtk_font_chooser_widget_ensure_matching_selection (GtkFontChooserWidget *self)
   if (desc_family == NULL)
     {
       gtk_single_selection_set_selected (self->selection, GTK_INVALID_LIST_POSITION);
-      return;
+      return TRUE;
     }
 
   n = g_list_model_get_n_items (G_LIST_MODEL (self->selection));
@@ -1397,7 +1403,13 @@ gtk_font_chooser_widget_ensure_matching_selection (GtkFontChooserWidget *self)
       pango_font_description_free (merged);
     }
 
-  gtk_single_selection_set_selected (self->selection, i);
+  if (i < n)
+    {
+      gtk_single_selection_set_selected (self->selection, i);
+      return TRUE;
+    }
+
+  return FALSE;
 }
 
 static PangoFontFace *
@@ -2352,6 +2364,7 @@ gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
   if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
               PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
     {
+      gtk_single_selection_set_selected (fontchooser->selection, GTK_INVALID_LIST_POSITION);
       gtk_font_chooser_widget_ensure_matching_selection (fontchooser);
     }